package com.topwalk.step01;

import lombok.extern.slf4j.Slf4j;

/**
 * 信号量
 *
 * @author tang
 * @since 2023/10/13
 */
@Slf4j
public class Demo5_4Signal {

    private static volatile int signal = 0;

    static class ThreadA implements Runnable {

        @Override
        public void run() {
            while (signal < 5) {
                if (signal % 2 == 0) {
                    log.debug("ThreadA：{}", signal);
                    signal++;
                }
            }
        }

    }

    static class ThreadB implements Runnable {

        @Override
        public void run() {
            while (signal < 5) {
                if (signal % 2 == 1) {
                    log.debug("ThreadB：{}", signal);
                    signal++;
                }
            }
        }

    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(new ThreadA()).start();
        Thread.sleep(0);
        new Thread(new ThreadB()).start();
    }

}
